package com.example.demo.lc.tree;

import java.util.ArrayList;
import java.util.List;
/**
* @author wzh
* @date 2021/7/22 5:24 下午
* @Description    前序遍历
 * https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
*/
public class LeetCode144 {
    public static void main(String[] args) {
        TreeNode root  = new TreeNode(1);
        root.left = null;
        TreeNode right  = new TreeNode(2);
        right.left = new TreeNode(3);
        root.right = right;
        System.out.println(preorderTraversal(root));
    }

    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        addNode(root,res);
        return res;
    }

    private static void addNode(TreeNode root, List<Integer> res) {
        if(root == null)return;
        res.add(root.val);
        addNode(root.left,res);
        addNode(root.right,res);
    }


}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
